package com.boshao.classschedule.activity;

import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import com.boshao.classschedule.ClassApplication;
import com.boshao.classschedule.R;
import com.boshao.classschedule.fragment.AbstartFragment;
import com.boshao.classschedule.fragment.MainFragment;

public class MainActivity extends FragmentActivity {
	private FragmentManager mFragmentManager;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		setDisplaySize();
		setupStatusBar();
		mFragmentManager = getSupportFragmentManager();
		if(savedInstanceState == null){
			AbstartFragment mainFragment = MainFragment.newInstance();
			replaceFragment(mFragmentManager,mainFragment,mainFragment.getTag(),false);
		}
	}
	
	/**
	 * 获取屏幕的宽高
	 */
	private void setDisplaySize(){
		DisplayMetrics displayMetrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
		ClassApplication.getInstance().setDisplayWidth(displayMetrics.widthPixels);
		ClassApplication.getInstance().setDisplayHeight(displayMetrics.heightPixels);
	}
	
	/**
	 * 沉浸式状态栏
	 */
	private void setupStatusBar() {
		View statusbar = findViewById(R.id.view_statusbar);
		if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
			getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
			statusbar.setVisibility(View.VISIBLE);
			RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
			            ClassApplication.getInstance().getStatusBarHeight()) ;
			statusbar.setLayoutParams(params);
		}else {
			statusbar.setVisibility(View.GONE);
		}
	}
	
	/**
	 * 添加Fragment View到MainActivity
	 * @param fragmentManager	
	 * @param fragment
	 * @param tag				fragment的Tag
	 * @param addToBackStack	是否加入栈
	 */
	public void addFragment(FragmentManager fragmentManager,AbstartFragment fragment,String tag,boolean addToBackStack){
		FragmentTransaction transaction = fragmentManager.beginTransaction();
		if(addToBackStack){
			transaction.addToBackStack(tag);
		}
		transaction.add(R.id.rl_main,(Fragment)fragment,fragment.getTag()).commitAllowingStateLoss();
		fragmentManager.executePendingTransactions();
	}
	
	/**
	 * 替换当前Fragment View
	 * @param fragmentManager
	 * @param fragment
	 * @param tag
	 * @param addToBackStack
	 */
	public void replaceFragment(FragmentManager fragmentManager,AbstartFragment fragment,String tag,boolean addToBackStack){
		FragmentTransaction transaction = fragmentManager.beginTransaction();
		if(addToBackStack){
			transaction.addToBackStack(tag);
		}
		transaction.replace(R.id.rl_main,(Fragment)fragment,fragment.getTag()).commitAllowingStateLoss();		
		fragmentManager.executePendingTransactions();
	}
	
	/**
	 * 获取当前栈的Fragment
	 * @param manager
	 * @return
	 */
	public Fragment getCurrentFragment(FragmentManager manager){
		int size = manager.getBackStackEntryCount();
		if(size == 0){
			return null;
		}
		FragmentManager.BackStackEntry backStackEntry = manager.getBackStackEntryAt(size -1);
		Fragment fragment = manager.findFragmentByTag(backStackEntry.getName());
		return fragment;
	}
	
	
	/**
	 * 点击回退键时调用
	 */
	@Override
	public void onBackPressed() {
		super.onBackPressed();
	}
			
}
